package com.bdqn.Demo.controller;

import com.alibaba.fastjson.JSON;
import com.bdqn.Demo.pojo.SysUser;
import com.bdqn.Demo.serivce.SysUserService;
import com.mysql.cj.util.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;

@Controller
@RequestMapping("/user")
public class SysUserController extends BaseController{

    @Resource
    private SysUserService sysUserService;

    private static final Logger logger=Logger.getLogger(SysUserController.class);

    @ResponseBody
    @GetMapping("/userExist")
    public Object userExist(@RequestParam String account){
        logger.debug("验证用户名account="+account+"的用户是否存在");
        HashMap<String,Object> resultMap=new HashMap<String, Object>();
        if(StringUtils.isNullOrEmpty(account)){
            resultMap.put("exist",1);
        }else{
            SysUser sysUser =sysUserService.getAccountExist(account);
            if (null!=sysUser) {
                resultMap.put("exist",1);
            }else{
                resultMap.put("exist",0);
            }
        }
        return JSON.toJSONString(resultMap);
    }

    @ResponseBody
    @GetMapping("/{id}/view")
    public Object view(@PathVariable String id){
        logger.debug("根据id查询用户详情id:"+id);
        SysUser sysUser=null;
        String sysUserJSON="";
        if (null==id||"".equals(id)) {
            return "nodata";
        }else{
            try {
//                 sysUser=sysUserService.getUserById(id);
                sysUserJSON=JSON.toJSONString(sysUser);
                logger.debug("用户信息转换为JSON:"+sysUserJSON);
            }catch (Exception e){
                e.printStackTrace();
                return "failed";
            }
            return sysUser;
        }
    }

}
